package beautiful.butterfly.drds.data_exchange.core.transport.transformer;


import common.element.Record;
import common.exception.DataExchangeException;
import groovy.lang.GroovyClassLoader;
import org.apache.commons.lang3.StringUtils;
import org.codehaus.groovy.control.CompilationFailedException;
import transformer.Transformer;

import java.util.Arrays;
import java.util.List;


public class GroovyTransformer extends Transformer
{
    private Transformer groovyTransformer;

    public GroovyTransformer()
    {
        setTransformerName("dx_groovy");
    }


    public Record evaluate(Record record, Object... paras)
    {

        if (groovyTransformer == null)
        {
            //全局唯一
            if (paras.length < 1 || paras.length > 2)
            {
                throw DataExchangeException.asDataExchangeException(TransformerErrorCode.TRANSFORMER_ILLEGAL_PARAMETER, "dx_groovy paras must be 1 or 2 . now paras is: " + Arrays.asList(paras).toString());
            }
            synchronized (this)
            {

                if (groovyTransformer == null)
                {
                    String expression = (String) paras[0];
                    @SuppressWarnings("unchecked") List<String> extraPackagesStringList = paras.length == 2 ? (List<String>) paras[1] : null;
                    initGroovyTransformer(expression, extraPackagesStringList);
                }
            }
        }

        return this.groovyTransformer.evaluate(record);
    }

    private void initGroovyTransformer(String expression, List<String> extraPackagesStringList)
    {
        GroovyClassLoader groovyClassLoader = new GroovyClassLoader(GroovyTransformer.class.getClassLoader());
        String groovyClassString = createGroovyClassString(expression, extraPackagesStringList);

        Class aClass;
        try
        {
            aClass = groovyClassLoader.parseClass(groovyClassString);
        } catch (CompilationFailedException cfe)
        {
            throw DataExchangeException.asDataExchangeException(TransformerErrorCode.TRANSFORMER_GROOVY_INIT_EXCEPTION, cfe);
        }

        try
        {
            Object newInstance = aClass.newInstance();
            if (!(newInstance instanceof Transformer))
            {
                throw DataExchangeException.asDataExchangeException(TransformerErrorCode.TRANSFORMER_GROOVY_INIT_EXCEPTION, "datax bug! contact askdatax");
            }
            this.groovyTransformer = (Transformer) newInstance;
        } catch (Throwable ex)
        {
            throw DataExchangeException.asDataExchangeException(TransformerErrorCode.TRANSFORMER_GROOVY_INIT_EXCEPTION, ex);
        }
    }


    private String createGroovyClassString(String expression, List<String> extraPackagesStringList)
    {
        StringBuffer sb = new StringBuffer();
        if (extraPackagesStringList != null)
        {
            for (String extraPackagesString : extraPackagesStringList)
            {
                if (StringUtils.isNotEmpty(extraPackagesString))
                {
                    sb.append(extraPackagesString);
                }
            }
        }
        sb.append("import static GroovyTransformerStaticUtil.*;");
        sb.append("import com.alibaba.datax.common.element.*;");
        sb.append("import DataExchangeException;");
        sb.append("import Transformer;");
        sb.append("import java.util.*;");
        sb.append("public class RULE extends Transformer").append("{");
        sb.append("public Record evaluate(Record record, Object... paras) {");
        sb.append(expression);
        sb.append("}}");

        return sb.toString();
    }


}
